package org.tmad.ocms.web;

import java.io.IOException;
import java.util.HashMap;
import org.tmad.ocms.util.CommonUtils;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import org.tmad.ocms.log.LogFactory;
import org.tmad.ocms.service.LoginService;

/**
 * Servlet implementation class LoginServlet
 * This servlet provides login functionality.
 */
@SuppressWarnings("unused")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LogFactory.getLogger();
	private LoginService service = null;

	/**
	 * Default constructor.
	 */
	public LoginServlet() {

	}

	/**
	 * this is GET implemnetation.
	 * @author harinath
	 * @param request The http request parameter.
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher("/login.jsp");
		dispatcher.forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
        service = new LoginService();
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		if (request.getParameter("forgot") != null) {
			try {
				service.sendPasswordDetails(email);
			} catch (Exception e) {
				logger.error("authentication failed for " + email, e);
				request.setAttribute("error", e.getMessage());
			}
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/forgotpwd.jsp");
			dispatcher.forward(request, response);
			return;

		}
		boolean loginStatus = false;
		try {
			loginStatus = service.login(email, password);
		} catch (Exception e) {
			logger.error("Error while login ", e);
			loginStatus =false;			
		}
		HttpSession session = request.getSession();
		if (loginStatus == true) {
			session.setAttribute("username", email);
			
			setMenuItems(session);
			      
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/home.jsp");
			dispatcher.forward(request, response);
		} else {
			logger.info("Login failed for user " + email);
			request.setAttribute("error", "User name or password is invalid");
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/login.jsp");
			dispatcher.forward(request, response);

		}
	}

	private void setMenuItems(HttpSession session) {
		Map<String, String> roleBasedMenu = new LinkedHashMap<String,String>();
		roleBasedMenu.put("index.jsp","Home Page");
		roleBasedMenu.put("newCase", "Submit new case");
		roleBasedMenu.put("casereg.jsp", "case details");
		roleBasedMenu.put("userlogin.jsp", "your profile");
		session.setAttribute("menu", roleBasedMenu);
	}

}
